<template>
	<view class="u-dropdown-item" v-if="active" @touchmove.stop.prevent="() => {}" @tap.stop.prevent="() => {}">
		<block v-if="!$slots.default && !$slots.$default">
			<scroll-view scroll-y="true" :style="{
				height: $u.addUnit(height)
			}">
				<view class="u-dropdown-item__options">
					<u-cell-group>
						<u-cell-item @click="cellClick(item.value)" :arrow="false" :title="item.label" v-for="(item, index) in options"
						 :key="index" :title-style="{
							color: value == item.value ? activeColor : inactiveColor
						}">
							<u-icon v-if="value == item.value" name="checkbox-mark" :color="activeColor" size="32"></u-icon>
						</u-cell-item>
					</u-cell-group>
				</view>
			</scroll-view>
		</block>
		<slot v-else />
	</view>
</template>

<script>
	
	export default {
		name: 'u-dropdown-item',
		props: {
			
			value: {
				type: [Number, String, Array],
				default: ''
			},
			
			title: {
				type: [String, Number],
				default: ''
			},
			
			options: {
				type: Array,
				default () {
					return []
				}
			},
			
			disabled: {
				type: Boolean,
				default: false
			},
			
			height: {
				type: [Number, String],
				default: 'auto'
			},
		},
		data() {
			return {
				active: false, 
				activeColor: '#2979ff', 
				inactiveColor: '#606266', 
			}
		},
		computed: {
			
			propsChange() {
				return `${this.title}-${this.disabled}`;
			}
		},
		watch: {
			propsChange(n) {
				
				
				if (this.parent) this.parent.init();
			}
		},
		created() {
			
			this.parent = false;
		},
		methods: {
			init() {
				
				let parent = this.$u.$parent.call(this, 'u-dropdown');
				if (parent) {
					this.parent = parent;
					
					this.activeColor = parent.activeColor;
					this.inactiveColor = parent.inactiveColor;
					
					
					let exist = parent.children.find(val => {
						return this === val;
					})
					if (!exist) parent.children.push(this);
					if (parent.children.length == 1) this.active = true;
					
					parent.menuList.push({
						title: this.title,
						disabled: this.disabled
					});
				}
			},
			
			cellClick(value) {
				
				this.$emit('input', value);
				
				this.parent.close();
				
				this.$emit('change', value);
			}
		},
		mounted() {
			this.init();
		}
	}
</script>

<style scoped lang="scss">
	@import "../../libs/css/style.components.scss";
</style>
